package com.example.medical.util;

import java.io.File;
import java.io.FileInputStream;
import java.security.KeyStore;

import javax.net.ssl.SSLContext;

import org.apache.commons.io.IOUtils;
import org.apache.http.HttpEntity;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustSelfSignedStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.springframework.http.ResponseEntity;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

/**
 * 
 * <pre>
 * 描述：趣医医院+SSL双向认证测试
 * 作者：lijunqiang 
 * 时间：2016年7月14日下午10:11:41
 * 类名: SSLClientDemo
 * </pre>
 */
public class QuyiSSLClientUtil {
    public static CloseableHttpClient acceptsCertsHttpClient() throws Exception {
        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        // ResourceUtils.getFile("classpath:resource/static/thirdPartyTrust/quyi/person-client.p12");

        keyStore.load(
                new FileInputStream(new File(
                        "D:/Users/lilei193/Downloads/siapp-city-web-develop-cd7a3859feb97cf0304e8d2056507053bd415b59/src/main/resources/static/thirdPartyTrust/quyi/person-client.p12")),
                "PARST310104.10074.hkbd@20170801".toCharArray());

        // ResourceUtils.getFile("classpath:resource/static/thirdPartyTrust/quyi/quyi-Cloud-truststore.jks");

        SSLContext sslcontext = SSLContexts.custom()
                // 加载服务端提供的truststore(如果服务器提供truststore的话就不用忽略对服务器端证书的校验了)
                .loadTrustMaterial(
                        new File(
                                "D:/Users/lilei193/Downloads/siapp-city-web-develop-cd7a3859feb97cf0304e8d2056507053bd415b59/src/main/resources/static/thirdPartyTrust/quyi/quyi-Cloud-truststore.jks"),
                        "quyicastore@20160706".toCharArray(), new TrustSelfSignedStrategy())
                .loadKeyMaterial(keyStore, "PARST310104.10074.hkbd@20170801".toCharArray()).build();
        SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslcontext,
                new String[] { "TLSv1", "TLSv1.1", "TLSv1.2" }, null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
        CloseableHttpClient httpclient = HttpClients.custom().setSSLSocketFactory(sslConnectionSocketFactory).build();
        return httpclient;
    }

    public static RestTemplate getRestTemplate() throws Exception {
        CloseableHttpClient httpClient = acceptsCertsHttpClient();
        HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                httpClient);
        // 设置连接超时和读超时时间
        clientHttpRequestFactory.setConnectTimeout(3000);
        clientHttpRequestFactory.setReadTimeout(30000);
        return new RestTemplate(clientHttpRequestFactory);
    }

    public static void main(String[] args) {
        RestTemplate restTemplate = null;
        try {
            CloseableHttpClient httpClient = acceptsCertsHttpClient();
            HttpComponentsClientHttpRequestFactory clientHttpRequestFactory = new HttpComponentsClientHttpRequestFactory(
                    httpClient);
            // 设置连接超时和读超时时间
            clientHttpRequestFactory.setConnectTimeout(3000);
            clientHttpRequestFactory.setReadTimeout(30000);
            restTemplate = new RestTemplate(clientHttpRequestFactory);
            String url = "https://open.quyiyuan.com:8443/v1.1/access/token/get?userName=PARST310104&password=CD968B8854AB2493";
            restTemplate.getForObject(url, HttpEntity.class);
            ResponseEntity<HttpEntity> response = restTemplate.getForEntity(url, HttpEntity.class);

            HttpEntity entity = response.getBody();
            System.out.println(response.getStatusCode());
            System.out.println(IOUtils.toString(entity.getContent()));
            // EntityUtils.consume(entity);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}